package com.bqtv.sport.commen.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import me.jessyan.autosize.utils.AutoSizeUtils;

public class LinearItemDecoration extends RecyclerView.ItemDecoration {
    private Paint mPaint;
    private int lineStart;//px 分割线开始
    private int lineWidth;//px 分割线宽
    private int lineHeight;//px 分割线宽
    private LineDrawMode drawMode;//行数

    /**
     * 分隔线绘制模式,水平，垂直，两者都绘制
     */
    public enum LineDrawMode {
        HORIZONTAL, VERTICAL
    }


    public LinearItemDecoration(Context context, LineDrawMode drawMode, float lineStartDp, float lineWidthDp, float lineHeightDp) {
        this.lineWidth = AutoSizeUtils.dp2px(context, lineWidthDp);
        this.lineHeight = AutoSizeUtils.dp2px(context, lineHeightDp);
        this.lineStart = AutoSizeUtils.dp2px(context, lineStartDp);
        this.drawMode = drawMode;

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.parseColor("#00000000"));
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        //left, top, right, bottom
        int childCount1 = parent.getChildCount();
        //        int childCount2 = parent.getLayoutManager().getChildCount();
        //        int childCount3 = parent.getAdapter().getItemCount();
        //        Log.e("count", "getChildCount()=" + childCount1 + "-----getLayoutManager().getChildCount()=" + childCount2 + "----getAdapter().getItemCount()=" + childCount3);
        for (int i = 0; i < childCount1; i++) {
            View child = parent.getChildAt(i);

            int itemPosition = ((RecyclerView.LayoutParams) child.getLayoutParams()).getViewLayoutPosition();

//            boolean[] sideOffsetBooleans = getItemSidesIsHaveOffsets(itemPosition);
            switch (drawMode) {
                case VERTICAL:
                    if (itemPosition == 0) {
                        drawChildTopHorizontal(child, c, parent);
                    }
                    drawChildBottomHorizontal(child, c, parent);
                    break;
                case HORIZONTAL:
                    if (itemPosition == 0) {
                        drawChildLeftVertical(child, c, parent);
                    }
                    drawChildRightVertical(child, c, parent);
                    break;
            }
        }
    }

    private void drawChildBottomHorizontal(View child, Canvas c, RecyclerView parent) {
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        int left = child.getLeft() - params.leftMargin - lineWidth;
        int right = child.getRight() + params.rightMargin + lineWidth;
        int top = child.getBottom() + params.bottomMargin;
        int bottom = top + lineHeight;

        c.drawRect(left, top, right, bottom, mPaint);
    }

    private void drawChildTopHorizontal(View child, Canvas c, RecyclerView parent) {
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        int left = child.getLeft() - params.leftMargin - lineWidth;
        int right = child.getRight() + params.rightMargin + lineWidth;
        int bottom = child.getTop() - params.topMargin;
        int top = bottom - lineHeight;

        c.drawRect(left, top, right, bottom, mPaint);
    }

    private void drawChildLeftVertical(View child, Canvas c, RecyclerView parent) {
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        int top = child.getTop() - params.topMargin - lineHeight;
        int bottom = child.getBottom() + params.bottomMargin + lineHeight;
        int right = child.getLeft() - params.leftMargin;
        int left = right - lineWidth;

        c.drawRect(left, top, right, bottom, mPaint);
    }

    private void drawChildRightVertical(View child, Canvas c, RecyclerView parent) {
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        int top = child.getTop() - params.topMargin - lineHeight;
        int bottom = child.getBottom() + params.bottomMargin + lineHeight;
        int left = child.getRight() + params.rightMargin;
        int right = left + lineWidth;

        c.drawRect(left, top, right, bottom, mPaint);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //outRect 看源码可知这里只是把Rect类型的outRect作为一个封装了left,right,top,bottom的数据结构,
        //作为传递left,right,top,bottom的偏移值来用的
        int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
//        boolean[] sideOffsetBooleans = getItemSidesIsHaveOffsets(itemPosition);

        //如果是设置左边或者右边的边距，就只设置成指定宽度的一半，
        // 因为这个项目中的 Grid 是一行二列，如果不除以二的话，那么中间的间距就会很宽，
        //可根据实际项目需要修改成合适的值

        int left = 0;
        int top = 0;
        int right = 0;
        int bottom = 0;

        switch (drawMode) {
            case VERTICAL:
                if (itemPosition == 0) {
                    top = lineStart;
                }
                bottom = lineWidth;
                break;
            case HORIZONTAL:
                if (itemPosition == 0) {
                    left = lineStart;
                }
                right = lineWidth;
                break;
        }
        outRect.set(left, top, right, bottom);
    }

    /**
     * 顺序:left, top, right, bottom
     *
     * @return boolean[4]
     */
    private boolean[] getItemSidesIsHaveOffsets(int itemPosition) {
        //顺序:left, top, right, bottom
        boolean[] booleans = {false, false, false, false};
        switch (drawMode) {
            case VERTICAL:
                booleans[3] = true;
                break;
            case HORIZONTAL:
                if (itemPosition == 0) {
                    booleans[0] = true;
                }
                booleans[2] = true;
                break;
        }

        return booleans;
    }
}